﻿<script lang="ts" name="products" setup>
import { ref, reactive, onMounted } from "vue";
import { ElMessage } from "element-plus";
import type { FormRules } from "element-plus";
import { formatDate } from '/@/utils/formatTime';
import { useproductsApi } from '/@/api/products/products';

//父级传递来的函数，用于回调
const emit = defineEmits(["reloadTable"]);
const productsApi = useproductsApi();
const ruleFormRef = ref();

const state = reactive({
	title: '',
	loading: false,
	showDialog: false,
	ruleForm: {} as any,
	stores: {},
	dropdownData: {} as any,
});

// 自行添加其他规则
const rules = ref<FormRules>({
  productsEncode: [{required: true, message: '请选择物料编码！', trigger: 'blur',},],
  productsName: [{required: true, message: '请选择物料名称！', trigger: 'blur',},],
  specificationsId: [{required: true, message: '请选择规格！', trigger: 'blur',},],
  modelId: [{required: true, message: '请选择型号！', trigger: 'blur',},],
  vendorId: [{required: true, message: '请选择供应商！', trigger: 'blur',},],
  expirationDate: [{required: true, message: '请选择有效日期！', trigger: 'change',},],
  meteringUnitId: [{required: true, message: '请选择计量单位！', trigger: 'blur',},],
  classifyId: [{required: true, message: '请选择分类！', trigger: 'blur',},],
  isEnable: [{required: true, message: '请选择是否启用！', trigger: 'blur',},],
});

// 页面加载时
onMounted(async () => {
  const data = await productsApi.getDropdownData(false).then(res => res.data.result) ?? {};
  state.dropdownData.specificationsId = data.specificationsId ?? [];
  state.dropdownData.modelId = data.modelId ?? [];
  state.dropdownData.vendorId = data.vendorId ?? [];
  state.dropdownData.meteringUnitId = data.meteringUnitId ?? [];
  state.dropdownData.classifyId = data.classifyId ?? [];
});

// 打开弹窗
const openDialog = async (row: any, title: string) => {
	state.title = title;
	row = row ?? {  };
	state.ruleForm = row.id ? await productsApi.detail(row.id).then(res => res.data.result) : JSON.parse(JSON.stringify(row));
	state.showDialog = true;
};

// 关闭弹窗
const closeDialog = () => {
	emit("reloadTable");
	state.showDialog = false;
};

// 提交
const submit = async () => {
	ruleFormRef.value.validate(async (isValid: boolean, fields?: any) => {
		if (isValid) {
			let values = state.ruleForm;
			await productsApi[state.ruleForm.id ? 'update' : 'add'](values);
			closeDialog();
		} else {
			ElMessage({
				message: `表单有${Object.keys(fields).length}处验证失败，请修改后再提交`,
				type: "error",
			});
		}
	});
};

//将属性或者函数暴露给父组件
defineExpose({ openDialog });
</script>
<template>
	<div class="products-container">
		<el-dialog v-model="state.showDialog" :width="800" draggable :close-on-click-modal="false">
			<template #header>
				<div style="color: #fff">
					<span>{{ state.title }}</span>
				</div>
			</template>
			<el-form :model="state.ruleForm" ref="ruleFormRef" label-width="auto" :rules="rules">
				<el-row :gutter="35">
					<el-form-item v-show="false">
						<el-input v-model="state.ruleForm.id" />
					</el-form-item>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="物料编码" prop="productsEncode">
							<el-input v-model="state.ruleForm.productsEncode" placeholder="请输入物料编码" maxlength="32" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="物料名称" prop="productsName">
							<el-input v-model="state.ruleForm.productsName" placeholder="请输入物料名称" maxlength="32" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="规格" prop="specificationsId">
							<el-select clearable filterable v-model="state.ruleForm.specificationsId" placeholder="请选择规格">
								<el-option v-for="(item,index) in state.dropdownData.specificationsId" :key="index" :value="item.value" :label="item.label" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="型号" prop="modelId">
							<el-select clearable filterable v-model="state.ruleForm.modelId" placeholder="请选择型号">
								<el-option v-for="(item,index) in state.dropdownData.modelId" :key="index" :value="item.value" :label="item.label" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="供应商" prop="vendorId">
							<el-select clearable filterable v-model="state.ruleForm.vendorId" placeholder="请选择供应商">
								<el-option v-for="(item,index) in state.dropdownData.vendorId" :key="index" :value="item.value" :label="item.label" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="有效日期" prop="expirationDate">
							<el-date-picker v-model="state.ruleForm.expirationDate" type="date" placeholder="有效日期" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="计量单位" prop="meteringUnitId">
							<el-select clearable filterable v-model="state.ruleForm.meteringUnitId" placeholder="请选择计量单位">
								<el-option v-for="(item,index) in state.dropdownData.meteringUnitId" :key="index" :value="item.value" :label="item.label" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="分类" prop="classifyId">
							<el-select clearable filterable v-model="state.ruleForm.classifyId" placeholder="请选择分类">
								<el-option v-for="(item,index) in state.dropdownData.classifyId" :key="index" :value="item.value" :label="item.label" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="是否启用" prop="isEnable">
							<el-switch v-model="state.ruleForm.isEnable" active-text="是" inactive-text="否" />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="() => state.showDialog = false">取 消</el-button>
					<el-button @click="submit" type="primary" v-reclick="1000">确 定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>
<style lang="scss" scoped>
:deep(.el-select), :deep(.el-input-number) {
  width: 100%;
}
</style>